
#include "GuideDuplicate.h"
#include "Common/EventDefine.h"
#include "Duplicate/DuplicateMgr.h"
#include "Character/Part.h"
#include "Character/monster/MonsterMgr.h"
#include "Character/monster/MonsterRefCfg.h"
#include "Character/CreatureMgr.h"
#include "Common/TableDataEx/MapCfg.h"
#include "Character/MovePart.h"
#include "Character/newAI/behaviac/CreatureCommonAI.h"

GuideDuplicate::GuideDuplicate()
{
	m_byType = (int8_t)EDuplicateType::EDuplicateType_Guide;
}

GuideDuplicate::~GuideDuplicate()
{
}

bool GuideDuplicate::Init(uint32_t nDuplicateID, uint32_t nSceneID, IBrushMonster *pBrush)
{	
	return Duplicate::Init(nDuplicateID, nSceneID, pBrush);;
}

//
void GuideDuplicate::UnInit()
{
	Duplicate::UnInit();
}

void GuideDuplicate::OnTimerUpdate(uint32_t nTimerID)
{
	switch (nTimerID)
	{
	case ETimer_Flop:
		{
			//翻牌结束
			OnCommon_FlopStateEnd();
		}
		break;
	case ETimer_Clean:
		{
			OnCommon_CleanStateEnd();
		}
		break;
	case ETimer_Destory:
		{
			LogDebugFmtPrint("[logic] GuideDuplicate destory .... duplicateid:%u , sceneid:%u, mapid:%u ", m_nDuplicateID, m_nSceneID, m_nMapID);
			OnCommon_DestoryStateEnd();
		}
		break;
	}
}

void GuideDuplicate::OnExecuteEvent(uint16_t nEventID, uint32_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
		{
		}
		break;
	case EVENT_ENTER_SCENE:
		{
		}
		break;
	case EVENT_DISCONNECT:
		{
		}
		break;
	case EVENT_FINISH_TASK:
		{
			FinishTaskEvent &taskEvent = *(FinishTaskEvent*)pContext;
			OnFinishTaskEvent(taskEvent);
		}
		break;
	}
}
//生物进入场景
void GuideDuplicate::OnCreatureEnter(Creature *pCreature)
{
	/*if (nullptr == pCreature)
	{
		return;
	}
	if (CREATURE_PLAYER == pCreature->CreatureKind())
	{
		//订阅角色刷怪事件
		if (!g_GetEvent()->Subscribe(this, EVENT_CREATURE_DEAD, pCreature->GetCid(), CREATURE_MONSTER, "NewDuplicate"))
		{
			LogErrFmtPrint("[logic] NewDuplicate::EnterDuplicate....Subscribe monster dead event failed....");
		}

		//订阅角色掉线事件
		if (!g_GetEvent()->Subscribe(this, EVENT_DISCONNECT, pCreature->GetCid(), CREATURE_PLAYER, "NewDuplicate"))
		{
			LogErrFmtPrint("[logic] NewDuplicate::EnterDuplicate....Subscribe player disconnect event  failed....");
		}

		//订阅角色死亡事件
		if (!g_GetEvent()->Subscribe(this, EVENT_CREATURE_DEAD, pCreature->GetCid(), CREATURE_PLAYER, "NewDuplicate"))
		{
			LogErrFmtPrint("[logic] NewDuplicate::EnterDuplicate....Subscribe player dead event  failed....");
		}

		LogDebugFmtPrint("[logic] NewDuplicate::EnterDuplicate success...cid:%u ,duplicateid:%u", pCreature->GetCid(), m_nDuplicateID);
	}*/
}

//是否能进入
bool GuideDuplicate::OnCanEnter(Creature *pCreature)
{
	//新手副本准备状态才能进入
	if (GetDuplicateState() == (int8_t)EDuplicateState::EDuplicateState_Ready)
	{
		if (m_setPlayer.size() < 1) //新手副本只允许一个人
		{
			return true;
		}
	}

	return false;
}

//进入区域
bool GuideDuplicate::OnEnterArea(uint32_t nAreaID)
{
	nAreaID = nAreaID;
	return true;
}

//离开副本
int32_t GuideDuplicate::LeaveDuplicate(Player *pPlayer, bool retMainCity/*= false*/)
{
	if (nullptr == pPlayer)
	{
		return RET_FAIL;
	}
	//没有完成新手引导副本，不能退出
	if (!pPlayer->IsFinishGuideDup())
	{
		return RET_DUPLICATE_FINISH_GUIDE_DUP_LIMIT;
	}
	return Duplicate::LeaveDuplicate(pPlayer, retMainCity);
}

//获取新手引导当前的步骤索引
int32_t GuideDuplicate::GetCurGuideIdx()
{
	//已完成的步骤
	uint32_t finishStep = m_paramEx.step;
	//这里需要返回当前的步骤，当前的步骤是已完成步骤的下一步骤
	uint32_t nextStep = finishStep + 1;
	//
	return (int32_t)(nextStep - 1);
}

//复活
bool GuideDuplicate::Relive(Player *pPlayer)
{
	if (nullptr == pPlayer)
	{
		return false;
	}
	if (!pPlayer->IsDead())
	{
		return false;
	}
	int32_t stepidx = GetCurGuideIdx();
	if (stepidx < 0 || stepidx > (int32_t)m_brushCfg.stNew.vecNewStep.size())
	{
		LogErrFmtPrint("GuideDuplicate::Relive...stepidx < 0 || stepidx > m_brushCfg.stNew.vecNewStep.size()...cid:%lu,dup:%u,scene:%u,map:%u,stepidx:%d,size:%d ", pPlayer->GetCid(), m_nDuplicateID, m_nSceneID, m_nMapID, stepidx, m_brushCfg.stNew.vecNewStep.size());
	}
	else
	{
		NewStepCfgInfo &info = m_brushCfg.stNew.vecNewStep.at(stepidx);
		const SBornPosCfgInfo *pBornPosCfg = g_GetMapCfgMgr()->GetBornCfgInfo(m_nMapID, info.bornId);
		if (nullptr == pBornPosCfg)
		{
			LogErrFmtPrint("GuideDuplicate::Relive...nullptr == pBornPosCfg...cid:%lu,dup:%u,scene:%u,map:%u,stepidx:%d,bornid:%u ", pPlayer->GetCid(), m_nDuplicateID, m_nSceneID, m_nMapID, stepidx,info.bornId);
		}
		else
		{
			MovePart *pMovePart = dynamic_cast<MovePart*>(pPlayer->GetPart(PART_MOVE));
			if (nullptr != pMovePart)
			{
				pMovePart->Teleporting(pBornPosCfg->bornpos);
				pPlayer->OnRevive(pPlayer->GetCid());
				//成功，直接返回
				return true;
			}
			else
			{
				LogErrFmtPrint("GuideDuplicate::Relive...nullptr == pMovePart...cid:%lu,dup:%u,scene:%u,map:%u,stepidx:%d,bornid:%u ", pPlayer->GetCid(), m_nDuplicateID, m_nSceneID, m_nMapID, stepidx, info.bornId);
			}
		}
	}

	//如果失败，直接走副本默认复活
	return Duplicate::Relive(pPlayer);
}

void GuideDuplicate::OnReady_State()
{
	
}

void GuideDuplicate::OnBegin_State()
{
	OnCommon_DupBeginState();
	//初始刷新引导和特效怪
	if (nullptr != m_pBrushMonster)
	{
		m_pBrushMonster->BrushGuideMonsGroup();
	}
}

void GuideDuplicate::OnFinish_State()
{
	//副本通关,新手引导步骤完成
	SET_UINT64 setPlayer = m_setPlayer;
	SET_UINT64::iterator iter = setPlayer.begin();
	for (; iter != setPlayer.end(); ++iter)
	{
		Player *pPlayer = GetPlayer((*iter));
		if (nullptr != pPlayer)
		{
			pPlayer->FinshAllGuideStep();
		}
	}

	OnCommon_DupFinishState();
}

void GuideDuplicate::OnFlop_State()
{
	//设置抽奖时间 20s
	LogDebugFmtPrint("[logic] GuideDuplicate::OnDuplicateState_Flop....duplicateid:%u , sceneid:%u ", m_nDuplicateID, m_nSceneID);
	OnCommon_DupFlopState();
}

void GuideDuplicate::OnClean_State()
{
	//删除抽奖定时器
	LogDebugFmtPrint("[logic] GuideDuplicate::OnClean_State....duplicateid:%u , sceneid:%u ", m_nDuplicateID, m_nSceneID);
	OnCommon_DupCleanState();
}

void GuideDuplicate::OnDestory_State()
{
	OnCommon_DupDestoryState();
}

//完成任务事件
void GuideDuplicate::OnFinishTaskEvent(FinishTaskEvent &taskEvent)
{
	Player *pPlayer = GetPlayer(taskEvent.cid);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("GuideDuplicate::OnFinishTaskEvent....nullptr == pPlayer, cid:%lu,dupid:%u, scene:%u, map:%u,taskid:%u ", taskEvent.cid, GetDuplicateID(), GetSceneID(), GetMapID(), taskEvent.taskId);
		return;
	}
	//查找当前任务对应的步骤
	uint32_t finisStep = m_paramEx.step;
	uint32_t curFinishStep = 0;
	uint32_t stepSize = m_brushCfg.stNew.vecNewStep.size();
	if (finisStep < stepSize) //finisStep >= stepSize表示已完成所有的步骤了
	{
		for (uint32_t i = finisStep; i < stepSize; ++i)
		{
			const NewStepCfgInfo &info = m_brushCfg.stNew.vecNewStep.at(i);
			if (info.missionId == taskEvent.taskId)
			{
				//本次完成的步骤
				curFinishStep = i + 1;
				break;
			}
		}

		//当前完成的步骤一定要比上一次完成的步骤大
		if (curFinishStep > finisStep)
		{
			//设置最近已完成的步骤
			pPlayer->FinishGuideDupStep(curFinishStep);
			LogDebugFmtPrint("GuideDuplicate::OnFinishTaskEvent....finish step, cid:%lu,dupid:%u, scene:%u, map:%u,finisStep:%u,stepSize:%u,curFinishStep:%u ", taskEvent.cid, GetDuplicateID(), GetSceneID(), GetMapID(), finisStep, stepSize, curFinishStep);
			m_paramEx.step = curFinishStep;
			if (curFinishStep >= stepSize)
			{
				//当前步骤大于等于最大的步骤，表示新手引导副本步骤全部完成
				pPlayer->FinshAllGuideStep();
				//
				LogDebugFmtPrint("GuideDuplicate::OnFinishTaskEvent....finish all step, cid:%lu,dupid:%u, scene:%u, map:%u,finisStep:%u,stepSize:%u,curFinishStep:%u ", taskEvent.cid, GetDuplicateID(), GetSceneID(), GetMapID(), finisStep, stepSize, curFinishStep);
			}
			//刷新下一步骤的引导和特效怪
			if (nullptr != m_pBrushMonster)
			{
				m_pBrushMonster->BrushGuideMonsGroup();
			}
		}
		else
		{
			LogErrFmtPrint("GuideDuplicate::OnFinishTaskEvent....curFinishStep <= finisStep, cid:%lu,dupid:%u, scene:%u, map:%u,finisStep:%u,stepSize:%u,curFinishStep:%u ", taskEvent.cid, GetDuplicateID(), GetSceneID(), GetMapID(), finisStep, stepSize, curFinishStep);
		}
	}
	else
	{
		LogErrFmtPrint("GuideDuplicate::OnFinishTaskEvent....finisStep > stepSize, cid:%lu,dupid:%u, scene:%u, map:%u,finisStep:%u,stepSize:%u ", taskEvent.cid, GetDuplicateID(), GetSceneID(), GetMapID(), finisStep, stepSize);
	}
	
	
}